/****************************************************
          File Name:loop_array_queue.h
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Mon 09 Feb 2015 10:15:17 PM CST
*****************************************************/

#ifndef loop_array_queue_h_
#define loop_array_queue_h_

// 算法复杂度 O(1)
#include <stdbool.h>

#define QUEUE_MAX   5

typedef int mydata;
typedef struct QUEUE
{
    mydata data[QUEUE_MAX];
    int front;
    int rear;
} Queue;

void InitQueue(Queue *q)
{
    q->front = 0;
    q->rear = 0;
}

bool isFull(Queue *q)
{
    return ((q->rear + 1) % QUEUE_MAX) == q->front ? true : false;
}

bool isEmpty(Queue *q)
{
    return q->front == q->rear ? true : false;
}

bool EnQueue(Queue *q, mydata e)
{
    if(isFull(q)) return false;

    q->data[q->rear] = e;
    q->rear = (q->rear + 1) % QUEUE_MAX;
    return true;
}

bool DeQueue(Queue *q, mydata *e)
{
    if(isEmpty(q)) return false;

    *e = q->data[q->front];
    q->front = (q->front + 1) % QUEUE_MAX;
    return true;
}

int QueueLength(Queue *q)
{
    return (q->rear - q->front + QUEUE_MAX) % QUEUE_MAX;
}

#endif
